package com.zqx.proxy.proxysubject;

import com.zqx.proxy.abstractsubject.Searcher;
import com.zqx.proxy.realsubject.RealSearcher;
import com.zqx.proxy.service.AccessValidator;
import com.zqx.proxy.service.Logger;

/**
 * description: 代理主题角色
 * @author zqx
 * @date 2018/09/04
 */
public class ProxySearcher implements Searcher {

    private RealSearcher realSearcher = new RealSearcher();//维持一个对真实主题的引用
    private AccessValidator validator;
    private Logger logger;
    @Override
    public String doSearch(String userId, String keyword) {

        if(this.validator(userId))
        {
            String search = realSearcher.doSearch(userId, keyword);//调用真实主题对象的查询方法
            this.log(userId);
            return search;
        }else
        {
            return null;
        }
    }

    //创建访问验证对象并调用其validate()方法实现身份验证
    public boolean validator(String userId)
    {
        validator = new AccessValidator();
        return validator.validate(userId);
    }

    //创建日志记录对象并调用其log()方法实现日志记录
    public void log(String userId)
    {
        logger = new Logger();
        logger.log(userId);
    }
}
